
from .models import *
from rest_framework import serializers
from rest_framework_recursive.fields import RecursiveField
# 用户表序列化器
class UserSerializer(serializers.ModelSerializer):
    rid=serializers.PrimaryKeyRelatedField(read_only=True)
    class Meta:
        model = User
        fields = '__all__'



# 权限表序列化器
class PermissionSerializer(serializers.ModelSerializer):
    class Meta:
        model = Permission
        fields = '__all__'
    def to_representation(self, instance):
        self.fields['children'] = PermissionSerializer(
            read_only=True, many=True)
        return super().to_representation(instance)


# 角色表序列化器
class RoleSerializer(serializers.ModelSerializer):
    # permission=PermissionSerializer(read_only=True,many=True)
    # permission_id=serializers.PrimaryKeyRelatedField(queryset=Permission.objects.all(),
    # write_only=True, many=True)
    user_set=UserSerializer(many=True,read_only=True)
    permission = PermissionSerializer(many=True)
    # 这种写法可以让返回的数据变成数组内容，而不是只是id，结合下面的create方法

    class Meta:
        model = Role
        fields = '__all__'

    # 重写create方法
    def create(self, validated_data):
        # 先从validated_data里删除掉关联内容数据
        per_datas = validated_data.pop('permission')
        # 创建当前表数据
        useRole = Role.objects.create(**validated_data)
        for per_data in per_datas:
            per_instance, created = Permission.objects.get_or_create(
                **per_data)
            useRole.permission.add(per_instance)
        return useRole

    # 重写update方法
    def update(self, instance, validated_data):
        per_datas = validated_data.pop('permission')
        for attr, value in validated_data.items():
            setattr(instance, attr, value)
        instance.save()
        for per_data in per_datas:
            Permission.objects.update(**per_data)
        return instance


# 岗位序列化器
class PostSerializer(serializers.ModelSerializer):
    class Meta:
        model = Post
        fields = '__all__'


# 部门表序列化器
class DepartSerializer(serializers.ModelSerializer):
    # children = serializers.ListField(source="get_children", child=RecursiveField())

    class Meta:
        model = Department
        fields = '__all__'

    def get_fields(self):
        fields = super(DepartSerializer, self).get_fields()
        fields['children'] = DepartSerializer(many=True)
        return fields
    # 重写create方法

    def create(self, validated_data):
        dep_datas = validated_data.pop('children')
        # 创建当前表数据
        useDep = Department.objects.create(**validated_data)
        for dep_data in dep_datas:
            per_instance, created = Department.objects.get_or_create(
                **dep_data)
            useDep.children.add(per_instance)
        return useDep
    # 重写update方法
    def update(self, instance, validated_data):
        dep_datas = validated_data.pop('children')
        for attr, value in validated_data.items():
            setattr(instance, attr, value)
        instance.save()
        for dep_data in dep_datas:
        # item {spec_id: "4", option_id: 8}
            Department.objects.filter(id=dep_data.get('id')).update(did=dep_data.get('did'),)
        return instance
